/**
* 头部菜单
*/ 
<template>
  <el-menu class="el-menu-demo" mode="horizontal" background-color="#334157" text-color="#fff" active-text-color="#fff">
    <el-button class="buttonimg">
      <img class="showimg" :src="collapsed?imgsq:imgshow" @click="toggle(collapsed)">
    </el-button>
    <el-submenu index="2" class="submenu">
      <template slot="title">{{loginNo}}</template>
      <el-menu-item @click="exit()" index="2-3">退出</el-menu-item>
    </el-submenu>
  </el-menu>
</template>
<script>
window.onbeforeunload = function(e){
     this.websock.close();   
};
let inetrval = null;
import { loginout } from '../api/userMG'
import  SockJS  from  'sockjs-client';
export default {
  name: 'navcon',
  data() {
    return {
      openVoice:'0',
      collapsed: true,
      imgshow: require('../assets/img/show.png'),
      imgsq: require('../assets/img/sq.png'),
      loginNo:'',
      websock:'',
    }
  },
  // 创建完毕状态(里面是操作)
  created() {
    this.loginNo = localStorage.getItem('loginNo')
    this.no = localStorage.getItem('no')
    if(this.loginNo){
      this.initWebSocket();
    }
    setInterval(() => {
      this.openVoice = sessionStorage.getItem('openVoice');
    },1000)
  },
  methods: {
    getVoice(){
      if(this.openVoice == '1'){
        new Audio(
          require('../assets/voice/text2audio.mp3')
        ).play();
      }
    },
    initWebSocket(){ //初始化weosocket
        const wsuri = "/api/message";
        this.websock = new SockJS(wsuri);
        this.websock.onmessage = this.websocketonmessage;
        this.websock.onopen = this.websocketonopen;
        this.websock.onerror = this.websocketonerror;
        this.websock.onclose = this.websocketclose;
        this.inetrval = setInterval(this.heartSend,20000);
      },
      heartSend(){
        let ln = localStorage.getItem('tokenId');
        if(ln){
          this.websock.send('-1');
        }else{
          clearInterval(this.inetrval);
          this.websock.close();
        }
      },
      websocketonopen(){ //连接建立之后执行send方法发送数据
        this.websock.send(this.no);
      },
      websocketonerror(){//连接建立失败重连
        this.initWebSocket();
      },
      websocketonmessage(e){ //数据接收
        this.getVoice();
      },
      websocketsend(Data){//数据发送
        this.websock.send(Data);
      },
      websocketclose(e){  //关闭
        console.log('断开连接',e);
        clearInterval(this.inetrval);
      },
    // 退出登录
    exit() {
      this.$confirm('退出登录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // setTimeout(() => {
          //   this.$store.commit('logout', 'false')
          //   this.websock.close() //退出断开websocket连接
          //   this.$router.push({ path: '/login' })
          //   this.$message({
          //     type: 'success',
          //     message: '已退出登录!'
          //   })
          // }, 1000)
          loginout()
            .then(res => {
              if (res.code == '1') {
                if(localStorage.getItem('tokenId')){
                  localStorage.removeItem('tokenId')
                }     
                //如果请求成功就让他2秒跳转路由
                this.$store.commit('logout', 'false')
                this.$router.push({ path: '/login' })
                this.$message({
                  type: 'success',
                  message: '已退出登录!'
                })
              } else {
                this.$message.error(res.msg)
                this.logining = false
                return false
              }
            })
            .catch(err => {
              if(localStorage.getItem('tokenId')){
                localStorage.removeItem('tokenId')
              }              this.logining = false
              this.$message.error('退出失败，请稍后再试！')
            })
        })
    },
    // 切换显示
    toggle(showtype) {
      this.collapsed = !showtype
      this.$root.Bus.$emit('toggle', this.collapsed)
    }
  }
}
</script>
<style scoped>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  border: none;
}
.submenu {
  float: right;
}
.buttonimg {
  height: 60px;
  background-color: transparent;
  border: none;
}
.showimg {
  width: 26px;
  height: 26px;
  position: absolute;
  top: 17px;
  left: 17px;
}
.showimg:active {
  border: none;
}
</style>